package com.jtlrm.ckd.mvp.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.base.sdk.base.net.CommonObserver;
import com.base.sdk.util.PhoneUtil;
import com.base.sdk.util.TuoMinUtil;
import com.base.sdk.util.ValidateUtils;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.base.acitvity.TitleBarActivity;
import com.jtlrm.ckd.entity.HuanZheBasicDataEntity;
import com.jtlrm.ckd.entity.HuanZheInfoEntity;
import com.jtlrm.ckd.entity.event.RemarkUpdate;
import com.jtlrm.ckd.mvp.model.HuanZheGuanLiModel;
import com.jtlrm.ckd.mvp.view.fragment.LiangBiaoPingGuFragment;
import com.jtlrm.ckd.mvp.view.fragment.ShengHuaCheckFragment;
import com.jtlrm.ckd.mvp.view.fragment.YinShiDataFragment;
import com.jtlrm.ckd.mvp.view.fragment.YinYangBaoGaoFragment;
import com.jtlrm.ckd.mvp.view.fragment.YinYangPingGuFragment;
import com.jtlrm.ckd.util.EventBusUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class HuanZheManagerActivity extends TitleBarActivity implements RadioGroup.OnCheckedChangeListener {
    private List<Fragment> fragments = new ArrayList<>();
    FragmentManager manager;
    @BindView(R.id.huanzhe_group)
    RadioGroup radioGroup;
    YinShiDataFragment yinShiDataFragment;// 饮食数据
    ShengHuaCheckFragment shengHuaCheckFragment; // 生化检查
    YinYangPingGuFragment yinYangPingGuFragment; // 营养评估
    LiangBiaoPingGuFragment liangBiaoPingGuFragment; // 量表评估
    YinYangBaoGaoFragment yinYangBaoGaoFragment; // 营养报告
    private int currentFragmentIndex = 0;
    @BindView(R.id.basic_manager)
    ImageView basicManager;
    @BindView(R.id.basic_info)
    LinearLayout basicInfo;
    @BindView(R.id.name)
    TextView nameT;
    @BindView(R.id.sex)
    TextView sexT;
    @BindView(R.id.age)
    TextView ageT;
    @BindView(R.id.phone)
    TextView phoneT;
    @BindView(R.id.idCard)
    TextView idCardT;
    @BindView(R.id.site)
    TextView siteT;
    @BindView(R.id.bao_xiao_style)
    TextView baoXiaoStyleT;
    @BindView(R.id.education)
    TextView educationT;
    @BindView(R.id.yuanfabing)
    TextView yuanFaBingT;
    @BindView(R.id.bansuibing)
    TextView banSuiBingT;
    @BindView(R.id.zhiliao_style)
    TextView zhiLiaoStyleT;
    @BindView(R.id.remark)
    RelativeLayout remarkR;
    @BindView(R.id.remark_content)
    TextView remarkT;
    HuanZheGuanLiModel huanZheGuanLiModel;
    String huanzheId;

    public static void goActivity(Context context, String id) {
        Intent intent = new Intent(context, HuanZheManagerActivity.class);
        intent.putExtra("id", id);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        huanzheId = getIntent().getStringExtra("id");
        super.onCreate(savedInstanceState);
    }

    @Override
    protected String getTitleText() {
        return "患者管理";
    }

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_huan_zhe_manager);
    }

    @Override
    protected void initView() {
        manager = getSupportFragmentManager();
        huanZheGuanLiModel = new HuanZheGuanLiModel();
        FragmentTransaction ft = manager.beginTransaction();
        if (yinShiDataFragment == null)
            yinShiDataFragment = YinShiDataFragment.newInstance(huanzheId);
        if (shengHuaCheckFragment == null)
            shengHuaCheckFragment = ShengHuaCheckFragment.newInstance(huanzheId);
        if (yinYangPingGuFragment == null)
            yinYangPingGuFragment = YinYangPingGuFragment.newInstance(huanzheId);
        if (liangBiaoPingGuFragment == null)
            liangBiaoPingGuFragment = LiangBiaoPingGuFragment.newInstance(huanzheId);
        if (yinYangBaoGaoFragment == null)
            yinYangBaoGaoFragment = YinYangBaoGaoFragment.newInstance(huanzheId);
        fragments.add(yinShiDataFragment);
        fragments.add(shengHuaCheckFragment);
        fragments.add(yinYangPingGuFragment);
        fragments.add(liangBiaoPingGuFragment);
        fragments.add(yinYangBaoGaoFragment);
        ft.add(R.id.main_container, fragments.get(currentFragmentIndex));
        ft.commit();
        EventBusUtil.register(this);
    }

    @Override
    protected void obtainData() {
        queryHuanZheBasic();
    }

    private void queryHuanZheBasic() {
        huanZheGuanLiModel.queryHuanZheBasic(huanzheId, new CommonObserver<HuanZheBasicDataEntity>() {
            @Override
            public void onError(int errType, String errMessage) {
                showToast(errMessage);
            }

            @Override
            public void onResult(HuanZheBasicDataEntity data) {
                setBasicView(data);
            }
        }, lifecycleSubject);
    }

    private void setBasicView(final HuanZheBasicDataEntity data) {
        if (data == null) {
            return;
        }
        setText(nameT, data.getName());
        setText(sexT, data.getSex());
        setText(ageT, data.getAge() + "岁");
        setText(phoneT, data.getMobile());
        setText(idCardT, TuoMinUtil.idEncrypt(data.getCardNo()));
        setText(siteT, data.getFullAddress());
        setText(baoXiaoStyleT, data.getExpenseType());
        setText(educationT, data.getEducation());
        setText(yuanFaBingT, data.getOrgDisease());
        setText(banSuiBingT, data.getWithDisease());
        setText(zhiLiaoStyleT, data.getMainTreatment());
        setText(remarkT, data.getRemarkCkd());
        remarkR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RemarkActivity.goAcitvity(context, huanzheId, true, data.getRemarkCkd() + "");
            }
        });
    }

    @Override
    protected void initEvent() {
        radioGroup.setOnCheckedChangeListener(this);
        basicManager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showBasic();
            }
        });
        phoneT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (notEmpty(getTextStr(phoneT)) && ValidateUtils.Mobile(getTextStr(phoneT))) {
                    PhoneUtil.diallPhone(context, getTextStr(phoneT));
                }
            }
        });
    }

    private void showBasic() {
        if (basicInfo != null) {
            if (basicInfo.isShown()) {
                basicManager.setImageResource(R.drawable.arrow_buttom);
                basicInfo.setVisibility(View.GONE);
            } else {
                basicManager.setImageResource(R.drawable.arrow_top);
                basicInfo.setVisibility(View.VISIBLE);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RemarkUpdate message) {
        if (message != null) {
            queryHuanZheBasic();
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int temp = 0;
        switch (checkedId) {
            case R.id.huanzhe_group_button_1:
                temp = 0;
                break;
            case R.id.huanzhe_group_button_2:
                temp = 1;
                break;
            case R.id.huanzhe_group_button_3:
                temp = 2;
                break;
            case R.id.huanzhe_group_button_4:
                temp = 3;
                break;
            case R.id.huanzhe_group_button_5:
                temp = 4;
                break;
        }
        if (temp == currentFragmentIndex) {
            return;
        }
        Fragment fragment = fragments.get(temp);
        FragmentTransaction ft = manager.beginTransaction();
        if (fragment.isAdded()) {
            ft.show(fragment);
        } else {
            ft.add(R.id.main_container, fragment);
        }
        ft.hide(fragments.get(currentFragmentIndex));
        currentFragmentIndex = temp;
        ft.commit();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBusUtil.unregister(this);
    }
}
